//---------------------------------------------------------------------------//
// Copyright (c) 2017-2024 Ismael Gutiérrez González. All rights reserved.
//
// This file is part of the Rusted PackFile Manager (RPFM) project,
// which can be found here: https://github.com/Frodo45127/rpfm.
//
// This file is licensed under the MIT license, which can be found here:
// https://github.com/Frodo45127/rpfm/blob/master/LICENSE.
//---------------------------------------------------------------------------//

use qt_widgets::QGridLayout;
use qt_widgets::QWidget;

use qt_core::QBox;
use qt_core::QPtr;
use qt_core::QString;

use anyhow::Result;

use std::rc::Rc;
use std::sync::{Arc, RwLock};

use rpfm_lib::files::{FileType, bmd::*};

use crate::app_ui::AppUI;
use crate::ffi::{cursor_row_safe, new_text_editor_safe, scroll_to_row_safe, set_text_safe};
use crate::packfile_contents_ui::PackFileContentsUI;
use crate::packedfile_views::{DataSource, FileView, View, ViewType};

use self::slots::FileBMDViewSlots;

mod connections;
mod slots;

//-------------------------------------------------------------------------------//
//                              Enums & Structs
//-------------------------------------------------------------------------------//

/// This struct contains the view of a BMD File.
pub struct FileBMDView {
    editor: QBox<QWidget>,
    packed_file_path: Option<Arc<RwLock<String>>>,
    data_source: Arc<RwLock<DataSource>>,
}

//-------------------------------------------------------------------------------//
//                             Implementations
//-------------------------------------------------------------------------------//

impl FileBMDView {

    /// This function creates a new BMD View, and sets up his slots and connections.
    pub unsafe fn new_view(
        file_view: &mut FileView,
        app_ui: &Rc<AppUI>,
        pack_file_contents_ui: &Rc<PackFileContentsUI>,
        data: &Bmd,
    ) -> Result<()> {

        let highlighting_mode = QString::from_std_str("JSON");
        let json_data = serde_json::to_string_pretty(data)?;
        let editor = new_text_editor_safe(&file_view.main_widget().static_upcast());
        let layout: QPtr<QGridLayout> = file_view.main_widget().layout().static_downcast();
        layout.add_widget_5a(&editor, 0, 0, 1, 1);

        set_text_safe(&editor.static_upcast(), &QString::from_std_str(json_data).as_ptr(), &highlighting_mode.as_ptr());

        let view = Arc::new(FileBMDView {
            editor,
            packed_file_path: Some(file_view.path_raw()),
            data_source: file_view.data_source.clone(),
        });

        let slots = FileBMDViewSlots::new(&view, app_ui, pack_file_contents_ui);
        connections::set_connections(&view, &slots);

        file_view.file_type = FileType::BMD;
        file_view.view_type = ViewType::Internal(View::Bmd(view));

        Ok(())
    }

    /// This function returns a pointer to the editor widget.
    pub fn get_mut_editor(&self) -> &QBox<QWidget> {
        &self.editor
    }

    /// Function to reload the data of the view without having to delete the view itself.
    pub unsafe fn reload_view(&self, data: &Bmd) {
        let highlighting_mode = QString::from_std_str("JSON");
        let json_data = serde_json::to_string_pretty(data).unwrap();
        let row_number = cursor_row_safe(&self.editor.as_ptr());
        set_text_safe(&self.editor.static_upcast(), &QString::from_std_str(json_data).as_ptr(), &highlighting_mode.as_ptr());

        // Try to scroll to the line we were before.
        scroll_to_row_safe(&self.editor.as_ptr(), row_number);
    }
}
